﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SimpleIR;
using System.Diagnostics.Contracts;

namespace RgnAnalyzer.Domain
{
	/// <summary>
	/// Simple struct for organizing possible pointer/ref targets in a BAPValue, APointer, or AReference Value when enumerating 
	/// graph structure, mapping for equality, or computing normal form.
	/// </summary>
	internal struct ATrgtEnum : IComparable<ATrgtEnum>
	{
		/// <summary>
		/// The access path to the location this is stored at (EmptyPath for non-structured local or eval stack origins).
		/// </summary>
		internal readonly IRBasicAccessPath BAPPath;
		
		/// <summary>
		/// The position of the APossiblePointer(Reference)Target in the APointer (AReference) that this came from.
		/// Use for looking up the needed APossiblePointer(Reference)Target in matching.
		/// </summary>
		internal readonly int PosKey;
		
		/// <summary>
		/// The target address for the ptr/ref.
		/// </summary>
		internal readonly int TrgtAddress;

		internal ATrgtEnum(int poskey, APossiblePointerTarget ptrgt)
		{
			this.BAPPath = ptrgt.SourceOffset;
			this.PosKey = poskey;

			this.TrgtAddress = ptrgt.PTarget;
		}

		internal ATrgtEnum(int poskey, APossibleReferenceTarget rtrgt)
		{
			this.BAPPath = IRBasicAccessPath.EmptyAccessPath;
			this.PosKey = poskey;

			this.TrgtAddress = rtrgt.RTarget;
		}

		/// <summary>
		/// ONLY ON SOURCE AND THE TARGET!!
		/// </summary>
		public static bool operator <(ATrgtEnum te1, ATrgtEnum te2)
		{ return te1.CompareTo(te2) < 0; }

		/// <summary>
		/// ONLY ON SOURCE AND THE TARGET!!
		/// </summary>
		public static bool operator >(ATrgtEnum te1, ATrgtEnum te2)
		{ return te1.CompareTo(te2) > 0; }

		/// <summary>
		/// ONLY ON SOURCE AND THE TARGET!!
		/// </summary>
		public int CompareTo(ATrgtEnum other)
		{
			int cmpv = this.BAPPath.CompareTo(other.BAPPath);
			return (cmpv != 0) ? cmpv : (this.TrgtAddress - other.TrgtAddress);
		}
	}
}
